import React from 'react';
import { NavContent, Link } from '../../ui-components';
import style from './style';

export
const HR_MANAGE_LIST = [
  { label: '新增入职', to: '#/hr/onboarding/onboarding-create' },
  { label: '处理中', to: '#/hr/onboarding/onboarding-flows?hasApproved=0' },
  { label: '已处理', to: '#/hr/onboarding/onboarding-flows?hasApproved=1' },
  { label: '入职模板', to: '#/hr/onboarding/onboarding-template' },
  { label: '入职文档', to: '#/hr/onboarding/onboarding-document' },
];

export
const HR_REORG_LIST = [
  { label: '转正管理', to: '#/hr/reorg/employ-managements/1' },
  { label: '调岗管理', to: '#/hr/reorg/transfer' },
  { label: '离职管理', to: '#/hr/reorg/employ-managements/2' },
];

export
const HR_TEAM_LIST = [
  { label: '组织架构管理', to: '#/hr/team/team-manage' },
  { label: '汇报关系管理', to: '#/hr/team/relation-manage' },
];

export
const HR_SETTING_LIST = [
  { label: '花名册字段设置', to: '#/hr/settings/profile-settings'},
  { label: '邀请员工', to: '#/hr/settings/onboarding-inviting?isActivated=-1' },
  { label: '补充花名册数据', to: '#/hr/settings/profile-import' }
]

export
const HR_NAV_LIST = [
  { label: '人事首页', to: teamId => teamId? `#/hr/index?teamId=${teamId}`: null },
  { label: '入职管理', to: '#/hr/onboarding/board', menu: HR_MANAGE_LIST },
  { label: '人事调整', to: '#/hr/reorg/board', menu: HR_REORG_LIST },
  { label: '组织调整', to: '#/hr/team/board', menu: HR_TEAM_LIST },
  { label: '设置', to: '#/hr/settings/board', menu: HR_SETTING_LIST },
];

export default
class Hr extends React.Component {

	static contextTypes = {
		router: React.PropTypes.object
	}

  render() {
    return (
      <div>
        {
          React.Children.map(this.props.children, child => React.cloneElement(child, {
            ...this.context
          })) 
        }
      </div>
    );
  }
}

export 
const HrMoudle =  list => class extends React.Component {

  isActive(url) {
    if (!url) return
    const { pathname, query } = this.props.location
    const { hasApproved }  = query
    if (hasApproved == null) {
      return pathname.indexOf(url.slice(1).split('?')[0]) === 0;
    }

    const match = /hasApproved=([^&])/.exec(url)
    return match && match[1] == hasApproved
  }

	static contextTypes = {
		router: React.PropTypes.object
	}

  render() {
    return (
			<NavContent 
        className={style.module}
        item={item => <Link active={this.isActive(item.to)} label={item.label} to={item.to}/>}
				navs={[list]} 
			>
        { 
          React.Children.map(this.props.children, child => React.cloneElement(child, {
            ...this.props.actions,
            ...this.props,
            ...this.context
          })) 
        }
			</NavContent>
    );
  }
}

export
const HrBoard = list => () => (
  <ul className={style.board}>
    {
      list.map(v => (
        <li key={v.label}>
          <Link label={ v.label } to={v.to} />
        </li>
      ))
    }
  </ul>
);
